package cn.swing.learning;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Desc : TODO
 * ClassName : cn.swing.learning.JDBCUitl
 *
 * @author : 林鹏群
 * @date : 2022/12/24 13:47
 */
public class JDBCUitl {

    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/blue?serverTimezone=Asia/Shanghai";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "password";

    public static void main(String[] args) {

        Connection connection = null;
        try {
            //1 加载驱动
            Class.forName(DRIVER_NAME);
            //2 获取链接
            connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            //3 创建sql的执行
            String sql = "select username from user";
            PreparedStatement statement = connection.prepareStatement(sql);
            //4 获取结果
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("username"));
            }
            //5 关闭
            resultSet.close();
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
